package com.excesys.exsecs.scada.service.impl;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.excesys.exsecs.scada.entity.ScadaEntity;
import com.excesys.exsecs.scada.entity.ScadaFolder;
import com.excesys.exsecs.scada.entity.ScadaProperty;
import com.excesys.exsecs.scada.entity.SysDictItem;
import com.excesys.exsecs.scada.mapper.ScadaFolderMapper;
import com.excesys.exsecs.scada.mapper.SysDictItemCopyMapper;
import com.excesys.exsecs.scada.service.ScadaFolderService;
import com.excesys.exsecs.scada.service.ScadaPropertyService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 文件夹_
 *
 * @author pig
 * @date 2024-06-25 09:18:13
 */
@Service
@RequiredArgsConstructor
public class ScadaFolderServiceImpl extends ServiceImpl<ScadaFolderMapper, ScadaFolder> implements ScadaFolderService {

	private final ScadaPropertyService scadaPropertyService;
	private final ScadaEntity scadaEntity;
	private final SysDictItemCopyMapper sysDictItemMapper;
	@Override
	public List<Tree<Long>> queryTree(ScadaFolder scadaFolder) {

		List<ScadaFolder> folderList = this.list(Wrappers.<ScadaFolder>lambdaQuery()
				.like(StringUtils.isNotEmpty(scadaFolder.getFolderName()),ScadaFolder::getFolderName,scadaFolder.getFolderName())
				.eq(ObjectUtil.isNotEmpty(scadaFolder.getId()),ScadaFolder::getId,scadaFolder.getId())
				.eq(StringUtils.isNotEmpty(scadaFolder.getType()),ScadaFolder::getType,scadaFolder.getType())
				.orderByAsc(ScadaFolder::getSort));

		List<TreeNode<Long>> collect = folderList.stream().map(getNodeFunction()).collect(Collectors.toList());
		if(ObjectUtil.isNotEmpty(scadaFolder.getId())){
			ScadaFolder label = this.getById(scadaFolder.getId());
			return TreeUtil.build(collect,label.getParentId());
		}
		return TreeUtil.build(collect,ObjectUtil.isNotNull(scadaFolder.getParentId())?scadaFolder.getParentId():-1L);
	}

	@Override
	public Boolean removeFoldersByIds(List<Long> ids) {
		recursiveDelete(ids);
		return true;
	}
	@Override
	public List<Tree<Long>> getFirstTree(ScadaFolder scadaFolder) {
		List<ScadaFolder> folderList = this.list(Wrappers.<ScadaFolder>lambdaQuery()
				.like(StringUtils.isNotEmpty(scadaFolder.getFolderName()),ScadaFolder::getFolderName,scadaFolder.getFolderName())
				.eq(ObjectUtil.isNotNull(scadaFolder.getParentId()),ScadaFolder::getParentId,scadaFolder.getParentId())
				.eq(ObjectUtil.isNotEmpty(scadaFolder.getId()),ScadaFolder::getId,scadaFolder.getId())
				.eq(StringUtils.isNotEmpty(scadaFolder.getType()),ScadaFolder::getType,scadaFolder.getType())
				.orderByAsc(ScadaFolder::getSort));

		List<TreeNode<Long>> collect = folderList.stream().map(getNodeFunction()).collect(Collectors.toList());
		if(ObjectUtil.isNotEmpty(scadaFolder.getId())){
			ScadaFolder label = this.getById(scadaFolder.getId());
			return TreeUtil.build(collect,label.getParentId());
		}
		return TreeUtil.build(collect,ObjectUtil.isNotNull(scadaFolder.getParentId())?scadaFolder.getParentId():-1L);
	}

	/*
		递归删除
	 */
	public void recursiveDelete(List<Long> ids){
		List<ScadaFolder> folders = this.listByIds(ids);
		if(CollectionUtils.isNotEmpty(folders)){
			this.removeBatchByIds(ids);
			List<ScadaFolder> childList = this.list(Wrappers.<ScadaFolder>lambdaQuery().in(ScadaFolder::getParentId, ids));
			List<Long> childIds = childList.stream().map(ScadaFolder::getId).collect(Collectors.toList());
			if(CollectionUtils.isNotEmpty(childIds)){
				recursiveDelete(childIds);
			}
		}
	}

	@NotNull
	private Function<ScadaFolder, TreeNode<Long>> getNodeFunction() {
		SysDictItem address = sysDictItemMapper.selectOne(Wrappers.<SysDictItem>lambdaQuery()
				.eq(SysDictItem::getDictType, "scada_address"));
		String scadaAddress = address.getItemValue();
		if(ObjectUtils.isEmpty(address) || StringUtils.isEmpty(scadaAddress)){
			scadaAddress = "127.0.0.1:8080";
		}
		//
		String finalScadaAddress = scadaAddress;
		return cloumn -> {
			TreeNode<Long> node = new TreeNode<>();
			node.setId(cloumn.getId());
			node.setName(cloumn.getFolderName());
			node.setParentId(cloumn.getParentId());
			node.setWeight(cloumn.getSort());
			// 扩展属性
			Map<String, Object> extra = new HashMap<>();
			extra.put("remark",cloumn.getRemark());
			extra.put("type",cloumn.getType().equals("0")?"文件夹":"标签");
			if(ObjectUtil.isNotEmpty(cloumn.getScadaId())){
				extra.put("scadaId", cloumn.getScadaId());
				ScadaProperty object = scadaPropertyService.getById(cloumn.getScadaId());
				String[] split = ObjectUtil.isNotNull(object.getFileName())?object.getFileName().split("\\."):new String[]{"nullName",""};
				extra.put("json",finalScadaAddress+"/meta2d/"+split[0]+"/meta2d-vue2/public/json/2d.json");
				extra.put("scadaName",object.getScadaName());
			}
			node.setExtra(extra);
			return node;
		};
	}

}